﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using TSqlFormatter;

namespace DevelopAssistant.Format
{
    public class TsqlFormatHelper
    {
        public static string FormatToTSQL(string strsql)
        {
            string result = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(strsql))
                    return strsql;

                bool errorsEncountered = false;
                result = SqlFormattingManager.DefaultFormat(strsql, ref errorsEncountered);

            }
            catch (Exception ex)
            {
                result = strsql;
            }

            return result;
        }

        public static string CompressToString(string strsql)
        {
            string result = string.Empty;

            strsql = strsql.Replace("//", "");
            strsql = strsql.Replace('\r', ' ');
            strsql = strsql.Replace('\n', ' ');
            //strsql = Regex.Replace(strsql, " +", " ", RegexOptions.None);     

            char[] char_array = strsql.ToCharArray();
            List<char> char_list = new List<char>();

            int space_count = 0;
            for (int i = 0, len = char_array.Length; i < len; i++)
            {
                char chr = char_array[i];
                if (chr == ' ')
                {
                    space_count++;
                    if (space_count == 1)
                    {
                        char_list.Add(chr);
                    }
                }
                if (chr != ' ')
                {
                    space_count = 0;
                    char_list.Add(chr);
                }

            }

            result =string.Concat(char_list.ToArray());

            return result;
        }

    }
}
